﻿using AutoMapper;
using B.Rabbit.DataBase.Domain;
using B.Rabbit.DataBase.ErrorCode;
using B.Rabbit.DataBase.Infrastructure.Infrastructure;
using B.Rabbit.DataBase.Read.Api.Applocation.Command.Rabbit.Traceab;
using B.Rabbit.DataBase.Read.Api.Applocation.Handler.Login;
using MediatR;

namespace B.Rabbit.DataBase.Read.Api.Applocation.Handler.Rabbit.Traceab
{
    public class DelTraceabCommandHandler : IRequestHandler<DelTraceabCommand, ApiResult<int>>
    {
        IBaseRepository<TraceabilityModel> traceabmodel;
        
        private readonly ILogger<DelTraceabCommandHandler> _logger;

        public DelTraceabCommandHandler(IBaseRepository<TraceabilityModel> traceabmodel, ILogger<DelTraceabCommandHandler> logger)
        {
            this.traceabmodel = traceabmodel;
            _logger = logger;
        }
        /// <summary>
        /// 删除溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DelTraceabCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            _logger.LogInformation("溯源信息删除操作");
            var traceab = await traceabmodel.GetValue(request.Id);
            traceab.IsDel = true;
            var deltraceab = await traceabmodel.UpdateAsync(traceab);
            if(deltraceab>0)
            {
                res.msg = "删除成功";
                res.code = StatusCode.Ok;
                res.data = deltraceab;
            }
            else
            {
                res.msg = "删除失败";
                res.code = StatusCode.Fail;
                res.data = deltraceab;
            }
            return res;
        }
    }
}
